package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpAssetsSearchPageUtil extends SeleniumPageUtil {

	public final static By ASS_SPAN_HEADER_TITLE 			= By.id("ctl44_hdrMain_lblHeader");
	public final static By ASS_LINK_SEARCH 						= By.xpath("//*[@id='ddcf634a-a5bf-4f11-9467-6e6d8e050e40']/a");
	public final static By ASS_LINK_CLEAR 						= By.xpath("//*[@id='dc8aa588-ce68-459b-a3a0-a7f5183bd3da']/a");
	public final static By ASS_TD_STATUS_CODE 				= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[1]");
	public final static By ASS_CHECKBOX_ACTIVE 				= By.id("ctl44_chkActive");
	public final static By ASS_LABEL_ACTIVE 						= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[2]/label[1]");
	public final static By ASS_CHECKBOX_INACTIVE 			= By.id("ctl44_chkInactive");
	public final static By ASS_LABEL_INACTIVE					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[2]/label[2]");
	public final static By ASS_CHECKBOX_PENDING 			= By.id("ctl44_chkPending");
	public final static By ASS_LABEL_PENDING 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[2]/label[3]");
	public final static By ASS_CHECKBOX_RETIRED				= By.id("ctl44_chkRetired");
	public final static By ASS_LABEL_RETIRED 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[2]/label[4]");
	public final static By ASS_CHECKBOX_OOS 					= By.id("ctl44_chkOOS");
	public final static By ASS_LABEL_OOS 							= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[1]/td[2]/label[5]");
	public final static By ASS_SPAN_SEARCH_IN 				= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[2]/td[1]");
	public final static By ASS_RADIO_MASTER					= By.id("ctl44_rdoSearchInCustomer");
	public final static By ASS_APSN_MASTER						= By.id("ctl44_lblCustomerName");
	public final static By ASS_RADIO_CUSTOMER				= By.id("ctl44_rdoSearchAllCustomers");
	public final static By ASS_APSN_CUSTOMER					= By.id("ctl44_lblAllCustomers");
	public final static By ASS_TD_ASSET_NAME 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[3]/td[1]");
	public final static By ASS_INPUT_ASSET_NAME				= By.id("ctl44_txtAssetName");
	public final static By ASS_TD_ASSET_TYPE 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[3]/td[3]");
	public final static By ASS_SELECT_ASSET_TYPE				= By.id("ctl44_cboAssetType");
	public final static By ASS_TD_SUB_TYPE 						= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[3]/td[5]");
	public final static By ASS_SELECT_SUB_TYPE					= By.id("ctl44_cboAssetSubType");
	public final static By ASS_TD_SERIAL 							= By.xpath("//*[@id='trSerial']/td[1]");
	public final static By ASS_INPUT_SERIAL_NUMBER			= By.id("ctl44_txtSerialNumber");
	public final static By ASS_TD_MDG_CODE 					= By.xpath("//*[@id='trSerial']/td[3]");
	public final static By ASS_SELECT_MDG_CODE 				= By.id("ctl44_cboMfgCode");
	public final static By ASS_TD_MODEL_CODE 				= By.xpath("//*[@id='trSerial']/td[5]");
	public final static By ASS_SELECT_MODEL_CODE 			= By.id("ctl44_lstModelCode");
	public final static By ASS_TD_ASSET_DESC 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[5]/td[1]");
	public final static By ASS_INPUT_ASSET_DESC 				= By.id("ctl44_txtAssetDescription");	
	public final static By ASS_TD_ASSET_GROUP 				= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[5]/td[3]");
	public final static By ASS_SELECT_ASSET_GROUP 			= By.id("ctl44_cboAssetGroup");	
	public final static By ASS_TD_ADD_NAME 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[6]/td[1]");
	public final static By ASS_SELECT_ADD_NAME 				= By.id("ctl44_cboAddress");
	public final static By ASS_TD_SG_LOCATION 				= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[6]/td[3]");
	public final static By ASS_SELECT_SG_LOCATION 			= By.id("ctl44_cbo5SegmentLocation");
	public final static By ASS_TD_OTHER_FIELD 					= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[7]/td[1]");
	public final static By ASS_SELECT_OTHER_FIELD			= By.id("ctl44_cboOtherField");
	public final static By ASS_TD_OTHER_VALUE 				= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[7]/td[3]");
	public final static By ASS_TD_USE_FORM 						= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[8]/td[1]");
	public final static By ASS_SELECT_USER_FORM 				= By.id("ctl44_cboUDF");
	public final static By ASS_TD_FIELD 							= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[8]/td[3]");
	public final static By ASS_SELECT_FIELD 						= By.id("ctl44_cboUDFField");
	public final static By ASS_TD_VALUE 							= By.xpath("//*[@id='form1']/table/tbody/tr[2]/td/div/table[2]/tbody/tr[8]/td[5]");
	public final static By ASS_SELECT_VALUE 						= By.id("ctl44_cboUDFValue");
	
	// element of result after search
	public final static By ASS_IMG_REFRESH = By.id("ctl44_AssetReport_Header_imgRefresh");
	public final static By ASS_SPAN_RESULT = By.id("ctl44_AssetReport_ReportOutput");
	
	public static void enterAssetsSearchPage(){
		SpHomePageUtil.enter();
		SeleniumUtil.click(SpHomePageUtil.getMainMenuAssets());
		SeleniumUtil.waitOnElements(new By[]{SpHomePageUtil.HOME_SUB_MENU_AST_SEARCH});
		SeleniumUtil.click(SpHomePageUtil.getAssetsSearch());
		waitForAssetsSearchPageToLoad();
	}
	public static void waitForAssetsSearchPageToLoad(){
		//asset search page in content area
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				ASS_LINK_SEARCH, ASS_LINK_CLEAR, ASS_CHECKBOX_INACTIVE, ASS_CHECKBOX_PENDING, ASS_SELECT_VALUE, ASS_SELECT_FIELD,
				ASS_SELECT_USER_FORM, ASS_SELECT_SG_LOCATION, ASS_SELECT_ASSET_GROUP, ASS_SELECT_MODEL_CODE, ASS_SELECT_SUB_TYPE,
				ASS_RADIO_MASTER, ASS_CHECKBOX_RETIRED, ASS_CHECKBOX_OOS, ASS_SELECT_ADD_NAME
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_LINK_SEARCH));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_LINK_CLEAR));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_CHECKBOX_INACTIVE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_CHECKBOX_PENDING));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_VALUE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_FIELD));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_USER_FORM));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_SG_LOCATION));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_ASSET_GROUP));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_MODEL_CODE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_SUB_TYPE));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_RADIO_MASTER));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_CHECKBOX_RETIRED));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_CHECKBOX_OOS));
		SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(ASS_SELECT_ADD_NAME));
	}
	
	public static void waitForResultsRefresh() {
		boolean flag = !SeleniumUtil.findElements(ASS_IMG_REFRESH).get(0).getAttribute("style").toLowerCase().contains("display: none");
		long before = System.currentTimeMillis();
		while(flag){
			if((System.currentTimeMillis() - before) > 60 * 1000){
				SeleniumUtil.fail("more than 30 seconds no response!");
				return;
			}else{
				flag = !SeleniumUtil.findElements(ASS_IMG_REFRESH).get(0).getAttribute("style").toLowerCase().contains("display: none");
			}
		}
	}
}
